package com.cjj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjj.common.convention.result.Result;
import com.cjj.common.convention.result.Results;
import com.cjj.dao.entity.ForumPost;
import com.cjj.dto.req.GetPublishedPostsReqDTO;
import com.cjj.dto.req.PostCreateReqDTO;
import com.cjj.dto.req.ReviewPostReqDTO;
import com.cjj.dto.resp.ForumPostDetailRespDTO;
import com.cjj.service.ForumPostService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/forumPost")
@RequiredArgsConstructor
public class ForumPostController {

    private final ForumPostService forumPostService;
    //private final RedisClient redisClient;

    /**
     * 创建帖子（默认待审核状态）
     */
    @PostMapping("/createPost")
    public Result<Void> createPost(@RequestBody PostCreateReqDTO postCreateReqDTO ) {
        forumPostService.createPost(postCreateReqDTO);
        return Results.success();
    }

    /**
     * 获取待审核帖子列表
     */
    @GetMapping("/getPendingPosts")
    public Result<IPage<ForumPost>> getPendingPosts(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {

        Page<ForumPost> posts = forumPostService.getPendingPosts(page, size);
        return Results.success(posts);
    }

    /**
     * 审核帖子
     */
    @PutMapping("/reviewPost")
    public Result<Void> reviewPost(@RequestBody ReviewPostReqDTO reviewPostReqDTO) {
        forumPostService.reviewPost(reviewPostReqDTO);
        return Results.success();
    }

    /**
     * 获取帖子列表（只返回已发布的），分页查询
     */
    @GetMapping("/getPublishedPosts")
    public Result<IPage<ForumPost>> getPublishedPosts(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody GetPublishedPostsReqDTO getPublishedPostsReqDTO
            ){
        Page<ForumPost> posts = forumPostService.getPublishedPosts(pageNum, pageSize,getPublishedPostsReqDTO);
        return Results.success(posts);
    }

    /**
     * 查看帖子详情
     */
    @GetMapping("/getPostDetail")
    public Result<ForumPostDetailRespDTO> getPostDetail(@RequestParam Long postId,
                                                        @RequestParam Long currentUserId){
        return Results.success(forumPostService.getPostDetail(postId,currentUserId));
    }
}
